package cn.dlc.com.test;

import cn.dlc.com.CoreApplication;
import cn.dlc.com.entity.*;
import cn.dlc.com.mapper.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

/**
 * @Author: dlc
 * @Date: 2021/12/22 - 11:22
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = CoreApplication.class)
public class Demo1 {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OtherMapper otherMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    CommonTableMapper commonTableMapper;

    @Autowired
    DictMapper dictMapper;

    @Test
    public void testInsert(){
        // 不需要指定id，他会自动生成一个纯数字的id
        Order order = new Order();
        order.setPrice(1.0);
        order.setStatus(1);
        order.setUserId(1L);
        int insert = orderMapper.insert(order);
        System.out.println(insert);
    }

    @Test
    public void testSelect(){
        // 不需要指定id，他会自动生成一个纯数字的id
        Order order = new Order();
        order.setOrderId(2L);

        QueryWrapper<Order> params = new QueryWrapper<>(order);
        Order order1 = orderMapper.selectOne(params);

        System.out.println(order1);
    }

    // 查询多个值(分库分表的查询)
    @Test
    public void testSelectMany(){

        QueryWrapper<Order> params = new QueryWrapper<>();
        params.in("order_id", 1L,2L);
        params.eq("user_id", 1L);

        List<Order> orders = orderMapper.selectList(params);

        System.out.println(orders);
    }


    // 测试垂直分库
    @Test
    public void testSelectUser(){

        QueryWrapper<User> params = new QueryWrapper<>();
        params.eq("user_id", 2L);

        List<User> users = userMapper.selectList(params);

        System.out.println(users);
    }


    // 测试公共表
    @Test
    public void testCommonTableInsert(){
        // 不需要指定id，他会自动生成一个纯数字的id
        CommonTable commonTable = new CommonTable();
        commonTable.setId(1L);
        commonTable.setName("test1");

        int insert = commonTableMapper.insert(commonTable);
        System.out.println(insert);
    }
    @Test
    public void testDictInsert(){
        // 不需要指定id，他会自动生成一个纯数字的id
        Dict dict = new Dict();
        dict.setId(1L);
        dict.setName("test1");

        int insert = dictMapper.insert(dict);
        System.out.println(insert);
    }

    @Test
    public void testOther(){
        otherMapper.insert(new Other(1L,"name"));
    }
}
